﻿using System;
using System.Windows.Forms;
using BillsDomain;

namespace Bills
{
    /// <summary>
    /// Wraps a System.Windows.Forms.ComboBox with an interface
    /// </summary>
    public class FormLookupList : ILookupList, IDisposable
    {
        /// <summary>
        /// The underlying ComboBox
        /// </summary>
        private ComboBox underlyingList;

        /// <summary>
        /// Initializes a new instance of the FormLookupList class.
        /// </summary>
        /// <param name="underlyingList"></param>
        public FormLookupList(ComboBox underlyingList)
        {
            this.underlyingList = underlyingList;
        }

        ~FormLookupList()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (underlyingList != null)
                {
                    underlyingList.Dispose();
                    underlyingList = null;
                }
            }
        }

        /// <summary>
        /// Adds the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        public void Add(object item)
        {
            this.underlyingList.Items.Add(item);
        }

        /// <summary>
        /// Clears this instance.
        /// </summary>
        public void Clear()
        {
            this.underlyingList.Items.Clear();
        }

        /// <summary>
        /// Gets or sets the selected item.
        /// </summary>
        /// <value>The selected item.</value>
        public object SelectedItem
        {
            get
            {
                object underlyingListSelectedItem = this.underlyingList.SelectedItem;
                if (underlyingListSelectedItem == null)
                {
                    underlyingListSelectedItem = underlyingList.Text;
                }

                return underlyingListSelectedItem;
            }
            set { this.underlyingList.SelectedItem = value; }
        }
    }
}
